﻿/*
 * Copyright 2014 Francois Karman
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

using Quartz.WebManager.Model;

namespace Quartz.WebManager.Web.Models
{
    /// <summary>
    /// Extends <see cref="JobInstance"/> to provide properties for web display
    /// </summary>
    public class WebJobInstance : JobInstance
    {
        private WebJobInstance(JobInstance instance)
        {
            this.Group = instance.Group;
            this.Name = instance.Name;
            this.JobDefinition = instance.JobDefinition;
            this.NextFireTime = instance.NextFireTime;
        }

        public string Key
        {
            get { return string.Format("{0}.{1}", this.Group, this.Name); }
        }

        public string DetailLink { get; set; }

        public string GroupLink { get; set; }

        public static WebJobInstance Create(JobInstance instance, System.Web.Mvc.UrlHelper helper)
        {
            return new WebJobInstance(instance)
            {
                DetailLink = helper.Action("Default", new { controller = "Home", action = "ViewJobDetail", group = instance.Group, name = instance.Name }),
                GroupLink = helper.Action("Default", new { controller = "Home", action = "ViewJobsByGroup", group = instance.Group }),
            };
        }

        public static WebJobInstance Create(JobInstance instance, System.Web.Http.Routing.UrlHelper helper)
        {
            return new WebJobInstance(instance)
            {
                DetailLink = helper.Link("Default", new { controller = "Home", action = "ViewJobDetail", group = instance.Group, name = instance.Name }),
                GroupLink = helper.Link("Default", new { controller = "Home", action = "ViewJobsByGroup", group = instance.Group }),
            };
        }
    }
}